package homework.module3;

import java.util.regex.Pattern;

public class StrCount {

    String targetStr = "ABCD123!@#$%ab";
    /**
     * 判断符合指定的正则表达式的字符数量
     * @param reg
     * @return
     */
    public static int countByReg(String[] strArray, String reg){

        int count = 0;  // 符合条件的字符数
        for (int i=0;i<strArray.length;i++){
            String perStr = strArray[i];
            if (perStr.matches(reg)){
                count++;
            }
        }
        return count;
    }


    public static void main(String[] args) {
        String targetStr = "ABCD123!@#$%ab";
        String[] targetArray = targetStr.split("");
        String upperReg = "[A-Z]";
        String lowerReg = "[a-z]";
        String numberReg = "[0-9]";

        int upperCount = countByReg(targetArray, upperReg);
        System.out.println("大写字母的数量：" + upperCount);

        int lowerCount = countByReg(targetArray, lowerReg);
        System.out.println("小写字母的数量：" + lowerCount);

        int numberCount = countByReg(targetArray, numberReg);
        System.out.println("数字的数量：" + numberCount);

        int otherCount = targetArray.length - upperCount - lowerCount - numberCount;
        System.out.println("其他字符的数量：" + otherCount);
    }
}
